/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmSearchItems.java
 *
 * Created on Mar 17, 2011, 9:48:14 AM
 */
package inventorymanagement;

import interact.iCheckForm;
import model.ColorTable;
import DataAccess.Customer;
import DataAccess.DBUtil;
import DataAccess.TableAccess;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Home
 */
public class frmSearchItems extends javax.swing.JFrame {

    /** Creates new form frmSearchItems */
    public frmSearchItems() {
        initComponents();
        this.setLocation();
        try {
            DataAccess.TableAccess.readToCombo("select * from Categories", cboItemCategory, "CategoryName");
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.refresh();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblSearchItems = new ColorTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtItemName = new javax.swing.JTextField();
        txtItemID = new javax.swing.JTextField();
        cboItemCategory = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        btnRefresh = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Search Items");
        setResizable(false);

        tblSearchItems.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblSearchItems);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(51, 153, 255), null));

        jLabel1.setText("Item ID:");

        jLabel2.setText("Item Name:");

        txtItemName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtItemNameKeyReleased(evt);
            }
        });

        txtItemID.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtItemIDKeyReleased(evt);
            }
        });

        cboItemCategory.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "All" }));
        cboItemCategory.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboItemCategoryItemStateChanged(evt);
            }
        });

        jLabel4.setText("Category:");

        btnRefresh.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Symbol-Refresh.png"))); // NOI18N
        btnRefresh.setText("Refresh");
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/searchCustomer.jpg"))); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addGap(34, 34, 34)
                            .addComponent(txtItemID, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addGap(18, 18, 18)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(cboItemCategory, 0, 190, Short.MAX_VALUE)
                                .addComponent(txtItemName))))
                    .addComponent(jLabel4)
                    .addComponent(btnRefresh))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 58, Short.MAX_VALUE)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(35, 35, 35))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtItemID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtItemName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(cboItemCategory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(btnRefresh))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(12, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 604, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
        // TODO add your handling code here:
        this.refresh();
        this.resetForm();
    }//GEN-LAST:event_btnRefreshActionPerformed



    private void txtItemIDKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtItemIDKeyReleased
        // TODO add your handling code here:
        String str = txtItemID.getText();
        String sql = "";
        System.out.println(cboItemCategory.getSelectedIndex());
        if (iCheckForm.checkNumber(str)) {
            JOptionPane.showMessageDialog(this, "CustomerID Invalid Format", "ERROR", JOptionPane.WARNING_MESSAGE);
            txtItemID.setText("");
            return;
        }
        if (str.equals("")) {
            if (cboItemCategory.getSelectedIndex() == 0) {
                //sql = "select * from Items";
                sql = "select Items.ItemID, Categories.CategoryName, Items.ItemName, Items.QuantityAvailable,"
                        + " Items.Price, Items.Unit, Items.Descript, Items.Manufacture, Items.Status from Items INNER JOIN Categories "
                        + "ON Items.CategoryID = Categories.CategoryID ";
            } else {
                sql = "select Items.ItemID, Categories.CategoryName, Items.ItemName, Items.QuantityAvailable,"
                        + " Items.Price, Items.Unit, Items.Descript, Items.Manufacture, Items.Status from Items INNER JOIN Categories "
                        + "ON Items.CategoryID = Categories.CategoryID "
                        + "where Items.CategoryID=" + cboItemCategory.getSelectedIndex();
            }
        } else {
            if (cboItemCategory.getSelectedIndex() == 0) {
                sql = "select Items.ItemID, Categories.CategoryName, Items.ItemName, Items.QuantityAvailable,"
                        + " Items.Price, Items.Unit, Items.Descript, Items.Manufacture, Items.Status from Items INNER JOIN Categories "
                        + "ON Items.CategoryID = Categories.CategoryID where ItemID=" + Integer.parseInt(str);

            } else {
                sql = "select Items.ItemID, Categories.CategoryName, Items.ItemName, Items.QuantityAvailable,"
                        + " Items.Price, Items.Unit, Items.Descript, Items.Manufacture, Items.Status from Items INNER JOIN Categories "
                        + "ON Items.CategoryID = Categories.CategoryID "
                        + "where ItemID='" + Integer.parseInt(str) + "' and Items.CategoryID=" + cboItemCategory.getSelectedIndex();
            }
        }


        try {
            DataAccess.Item.readToTableItem(sql, tblSearchItems);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_txtItemIDKeyReleased

    private void txtItemNameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtItemNameKeyReleased
        // TODO add your handling code here:
        String str = txtItemName.getText();
        String sql = "";
        if (str.equals("")) {
            if (cboItemCategory.getSelectedIndex() == 0) {
                //sql = "select * from Items";
                sql = "select Items.ItemID, Categories.CategoryName, Items.ItemName, Items.QuantityAvailable,"
                        + " Items.Price, Items.Unit, Items.Descript, Items.Manufacture, Items.Status from Items INNER JOIN Categories "
                        + "ON Items.CategoryID = Categories.CategoryID ";
            } else {
                sql = "select Items.ItemID, Categories.CategoryName, Items.ItemName, Items.QuantityAvailable,"
                        + " Items.Price, Items.Unit, Items.Descript, Items.Manufacture, Items.Status from Items INNER JOIN Categories "
                        + "ON Items.CategoryID = Categories.CategoryID "
                        + "where Items.CategoryID=" + cboItemCategory.getSelectedIndex();
            }
        } else {
            if (cboItemCategory.getSelectedIndex() == 0) {
                sql = "select Items.ItemID, Categories.CategoryName, Items.ItemName, Items.QuantityAvailable,"
                        + " Items.Price, Items.Unit, Items.Descript, Items.Manufacture, Items.Status from Items INNER JOIN Categories "
                        + "ON Items.CategoryID = Categories.CategoryID "
                        + "and ItemName like '%" + str + "%'";
            } else {
                sql = "select Items.ItemID, Categories.CategoryName, Items.ItemName, Items.QuantityAvailable,"
                        + " Items.Price, Items.Unit, Items.Descript, Items.Manufacture, Items.Status from Items INNER JOIN Categories "
                        + "ON Items.CategoryID = Categories.CategoryID "
                        + "where Items.CategoryID='" + cboItemCategory.getSelectedIndex() + "' "
                        + "and ItemName like '%" + str + "%'";
            }
        }

        try {
            DataAccess.Item.readToTableItem(sql, tblSearchItems);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }//GEN-LAST:event_txtItemNameKeyReleased

    private void cboItemCategoryItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboItemCategoryItemStateChanged
        // TODO add your handling code here:
//        String cateName = cboItemCategory.getSelectedItem().toString();
//        int cateid = 0;
//        if (cateName.equals("All")) {
//            try {
//                DataAccess.Item.readToTableItem("select Items.ItemID, Categories.CategoryName, Items.ItemName, Items.QuantityAvailable,"
//                        + " Items.Price, Items.Unit, Items.Descript, Items.Manufacture, Items.Status from Items INNER JOIN Categories "
//                        + "ON Items.CategoryID = Categories.CategoryID ", tblSearchItems);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        } else {
//            try {
//                String sql = "select CategoryID from Categories where CategoryName='" + cateName + "'";
//                Statement st = DBUtil.getConnection(DBUtil.getInfoDB()).createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
//                //ResultSet rs = DataInteraction.queryResultSet("select CategoryID from Categories where CategoryName='" + cateName+"'");
//                ResultSet rs = st.executeQuery(sql);
//                while (rs.next()) {
//                    cateid = rs.getInt("CategoryID");
//                }
//            } catch (SQLException ex) {
//                Logger.getLogger(frmSearchItems.class.getName()).log(Level.SEVERE, null, ex);
//            }
//            String sql = "select * from Items where CategoryID=" + cateid;
//            try {
//                DataAccess.Item.readToTableItem(sql, tblSearchItems);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
        if (cboItemCategory.getSelectedIndex() == 0) {
            try {
                DataAccess.Item.readToTableItem("select Items.ItemID, Categories.CategoryName, Items.ItemName, Items.QuantityAvailable,"
                        + " Items.Price, Items.Unit, Items.Descript, Items.Manufacture, Items.Status from Items INNER JOIN Categories "
                        + "ON Items.CategoryID = Categories.CategoryID ", tblSearchItems);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                DataAccess.Item.readToTableItem("select Items.ItemID, Categories.CategoryName, Items.ItemName, Items.QuantityAvailable,"
                        + " Items.Price, Items.Unit, Items.Descript, Items.Manufacture, Items.Status from Items INNER JOIN Categories "
                        + "ON Items.CategoryID = Categories.CategoryID "
                        + "where Items.CategoryID=" + cboItemCategory.getSelectedIndex(), tblSearchItems);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }//GEN-LAST:event_cboItemCategoryItemStateChanged

    private void setLocation() {
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) ((d.getWidth() - getWidth()) / 2);
        int y = (int) ((d.getHeight() - getHeight()) / 2);
        setLocation(x, y);
    }

    /**
     * @param args the command line arguments
     */
    private void refresh() {
        try {
            DataAccess.Item.readToTableItem("select Items.ItemID, Categories.CategoryName, Items.ItemName, Items.QuantityAvailable,"
                        + " Items.Price, Items.Unit, Items.Descript, Items.Manufacture, Items.Status from Items INNER JOIN Categories "
                        + "ON Items.CategoryID = Categories.CategoryID ", tblSearchItems);
            //DataAccess.TableAccess.readToCombo("select * from Categories", cboItemCategory, "CategoryName");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void resetForm() {
        txtItemID.setText("");
        txtItemName.setText("");
        //txtItemPrice.setText("");
        cboItemCategory.setSelectedIndex(0);
    }

    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(frmSearchItems.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(frmSearchItems.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(frmSearchItems.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(frmSearchItems.class.getName()).log(Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new frmSearchItems().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnRefresh;
    private javax.swing.JComboBox cboItemCategory;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblSearchItems;
    private javax.swing.JTextField txtItemID;
    private javax.swing.JTextField txtItemName;
    // End of variables declaration//GEN-END:variables
}
